﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Krile.Cores;

namespace Krile.Forms.ConfigForm.Children
{
    public partial class PluginColorOverride : Krile.Kernel.Control.ConfigPage
    {
        Cores.Config._PluginCommon.ServiceColor svc;
        public PluginColorOverride(string serviceId)
        {
            svc = Core.Config.PluginCommon.GetServiceColor(serviceId);
            if (svc == null)
            {
                svc = new Krile.Cores.Config._PluginCommon.ServiceColor();
                var service = Core.PluginManager.LookupService(serviceId);
                if (service == null)
                    throw new KrileFatalException("Unexisted service loaded.");
                svc.ServiceBackColor = new Krile.Imported.Data.XColor(service.ServiceColor);
                svc.ServiceForeColor = new Krile.Imported.Data.XColor(service.ForeColor);
                svc.ServiceSecondaryColor = new Krile.Imported.Data.XColor(service.SecondaryColor);
                svc.ServiceId = serviceId;
            }
            InitializeComponent();
            UpdateColors();
        }

        public void UpdateColors()
        {
            userNameBack.BackColor = svc.ServiceBackColor.Value;
            userNameLabel.ForeColor = svc.ServiceForeColor.Value;
            selectedBack.BackColor = svc.ServiceSecondaryColor.Value;
        }

        private void selServiceColor_Click(object sender, EventArgs e)
        {
            using (var cd = new ColorDialog())
            {
                cd.Color = svc.ServiceBackColor.Value;
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    svc.ServiceBackColor.Value = cd.Color;
                    UpdateColors();
                }
            }
        }

        private void selServiceForeColor_Click(object sender, EventArgs e)
        {
            using (var cd = new ColorDialog())
            {
                cd.Color = svc.ServiceForeColor.Value;
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    svc.ServiceForeColor.Value = cd.Color;
                    UpdateColors();
                }
            }
        }

        private void selServiceSecondColor_Click(object sender, EventArgs e)
        {
            using (var cd = new ColorDialog())
            {
                cd.Color = svc.ServiceSecondaryColor.Value;
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    svc.ServiceSecondaryColor.Value = cd.Color;
                    UpdateColors();
                }
            }
        }

        public override void Apply()
        {
            Core.Config.PluginCommon.SetServiceColor(svc);
        }

        private void setDefault_Click(object sender, EventArgs e)
        {
            var serviceId = svc.ServiceId;
            svc = new Krile.Cores.Config._PluginCommon.ServiceColor();
            var service = Core.PluginManager.LookupService(serviceId);
            if (service == null)
                throw new KrileFatalException("Unexisted service loaded.");
            svc.ServiceBackColor = new Krile.Imported.Data.XColor(service.ServiceColor);
            svc.ServiceForeColor = new Krile.Imported.Data.XColor(service.ForeColor);
            svc.ServiceSecondaryColor = new Krile.Imported.Data.XColor(service.SecondaryColor);
            svc.ServiceId = serviceId;
            UpdateColors();
        }
    }
}
